import 'dart:io';
import 'dart:math';
import 'package:flutter/widgets.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_image/flutter_native_image.dart';

import 'log_utils.dart';

class CompressTool {
  static Future<File> compressImage(File fire) async {
    if (getPrintSize(fire.lengthSync()) > 500) {
      fire = await FlutterNativeImage.compressImage(fire.path, quality: 80);
      return compressImage(fire);
    } else {
      return fire;
    }
  }

  static getPrintSize(limit) {
    String size = "";
    size = (limit / 1024).toString();
    size = size.substring(0, size.indexOf(".") + 3);
    return double.parse(size);
  }

  //压缩图片
  //判断图片大小是否需要压缩
  //type
  //1、头像 2M
  //2、动态 2.5M
  static Future<File> lyCompress(File fire, int type) async {
    int byte = await fire.length();
    LogUtilTool.println(byte);
    if (compareSize(type, byte)) {
      int scale = calScale(type, byte);
      LogUtilTool.println(scale);
      fire = await FlutterNativeImage.compressImage(fire.path, percentage: scale);
      int byte1 = await fire.length();
      LogUtilTool.println(byte1);
    }
    return fire;
  }

  //计算压缩比
  static int calScale(int type, int byte) {
    if (type == 1) {
      return (1024 * 1.5 * 1024 * 100/ byte).round();
    } else if (type == 2) {
      return (1024 * 2.5 * 1024 * 100/ byte).round();
    }
    return 80;
  }

  //判断图片大小是否需要压缩
  //type
  //1、头像 1M
  //2、动态 1.5M
  static bool compareSize(int type, int byte) {
    if (type == 1) {
      return byte > 1024 * 1024;
    } else if (type == 2) {
      return byte > 1024 * 1.5 * 1024;
    }
    return false;
  }

  // ///选择头像(单张裁剪)
  // static Future<MultipartFile?> selectHeadIcon(BuildContext context) async {
  //   var pickedFile =
  //       // ignore: invalid_use_of_visible_for_testing_member
  //       await ImagePicker.platform.pickImage(source: ImageSource.gallery);
  //
  //   final file = File(pickedFile!.path);
  //   // final sample = await ImageCrop.sampleImage(
  //   //   file: file,
  //   //   preferredSize: context.size!.longestSide.ceil(),
  //   // );
  //   var result = await Navigator.of(context)
  //       .pushNamed(RoutePaths.CROP_IMAGE, arguments: {
  //     // 'sample': sample,
  //     'file': file,
  //   });
  //   if (result != null) {
  //     File fire = await lyCompress(result as File, 1);
  //     MultipartFile fired = await MultipartFile.fromFile(fire.path,
  //         filename: '${DateTime.now().millisecondsSinceEpoch}.png');
  //     return fired;
  //   }
  // }
}

class ImageHelper {
  static const String baseUrl = 'http://www.meetingplus.cn';
  static const String imagePrefix = '$baseUrl/uimg/';

  static String wrapUrl(String url) {
    if (url.startsWith('http')) {
      return url;
    } else {}
    return imagePrefix + url;
  }

  static String wrapAssets(String url) {
    return "assets/images/" + url;
  }

  static Widget placeHolder(double width, double height) {
    return SizedBox(
        width: width,
        height: height,
        child: CupertinoActivityIndicator(radius: width / 3
            // min(10.0, width/ 3)
            ));
  }

  static Widget error(double width, double height, double size) {
    return SizedBox(
        width: width,
        height: height,
        child: Icon(
          Icons.error_outline,
          size: size,
        ));
  }

  static String randomUrl(
      {int width = 100, int height = 100, Object key = ''}) {
    return 'http://placeimg.com/$width/$height/${key.hashCode.toString() + key.toString()}';
  }
}
